package com.im.holder;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Presence;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.core.sdk.ui.holder.BaseViewHolder;
import com.im.R;
import com.im.app.ImApplication;

public class RosterEntityHolder extends BaseViewHolder<RosterEntry, RosterEntityStatus> {

	TextView tvName;
	TextView tvStatus;
	
	@Override
	protected void onInitViews(View view) {
		tvName = find(R.id.item_roster_name);
		tvStatus = find(R.id.item_roster_status);
	}

	@Override
	protected void onBindItem() {
		bindText();
	}

	private void bindText() {
		RosterEntry re = getItem().getData();
		if(re == null)
			return ;
		String name = re.getName() == null ? "NULL" : re.getName();
		tvName.setText(name);
		Presence pre = ((ImApplication)getRootView().getContext().getApplicationContext()).getRoster().getPresence(re.getUser());
		if(pre == null || pre.getType().ordinal() != Presence.Type.available.ordinal()){
			tvStatus.setText("离线");
		}else{
			tvStatus.setText("在线");
		}
	}

	@Override
	protected void onResetViews() {
		tvName.setText("");
		tvStatus.setText("");
	}

	@Override
	protected void onRecycleItem() {
	
	}

	@Override
	protected void onRefreshView() {
		bindText();
	}

	@Override
	protected void onDestroy() {
		tvName = null;
		tvStatus = null;
	}
	
	public RosterEntityHolder(View rootView,Context act) {
		super(rootView,act);
	}
	

}
